package com.huiren.ui.configUI;

import javax.annotation.PostConstruct;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.huiren.util.JdbcUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;

@Component
public class CreateProcedureJPanel extends JPanel {

	private static Logger logger = Logger
			.getLogger(CreateProcedureJPanel.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextArea createSql;
	private JLabel result;
	private JLabel params;

	public CreateProcedureJPanel() {
		setLayout(null);

		JLabel lblNewLabel = new JLabel("存储过程名称:hr_importData");
		lblNewLabel.setBounds(43, 19, 161, 23);
		add(lblNewLabel);

		JButton createButton = new JButton("创建");
		createButton.setBounds(175, 520, 93, 23);
		add(createButton);
		createButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int type = JOptionPane.showConfirmDialog(null, "您确定要保存吗?",
						"提示", JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE);
				if (type == JOptionPane.YES_NO_OPTION) {
					createProcedure();
					logger.info("创建存储过程");
				}

			}

		});

		JButton testButton = new JButton("测试");
		testButton.setBounds(538, 520, 93, 23);
		add(testButton);

		JPanel panel = new JPanel();
		panel.setBounds(43, 66, 720, 433);
		add(panel);
		panel.setLayout(new CardLayout(0, 0));
		createSql = new JTextArea();
		createSql.setLineWrap(true);
		JScrollPane js = new JScrollPane(createSql);
		panel.add(js, "name_24451418464680");
		js.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		result = new JLabel("");
		result.setForeground(Color.RED);
		result.setBounds(554, 23, 297, 15);
		add(result);

		params = new JLabel("params");
		params.setBounds(214, 23, 549, 15);
		add(params);
		this.getParams();
	}

	@PostConstruct
	public void initMethod() {

	}

	public void resetCreateSql() {
		List<Map<String, Object>> sql = JdbcUtil
				.executeQuery("select * from T_YJ_CONFIG where key ='createProcedure'");
		createSql.setText(sql.get(0).toString());
	}

	// 获取传入参数
	public void getParams() {
		List<Map<String, Object>> groupResult = JdbcUtil
				.executeQuery("select * from T_YJ_CONFIG where key ='group'");
		if (groupResult.size() != 0) {
			String groupStr = String.valueOf(groupResult.get(0).get("value"))
					.toLowerCase();

			params.setText("(" + groupStr + ")");
		}
	}

	// 执行创建sql
	private void createProcedure() {
		if (JdbcUtil.executeUpdate(createSql.getText()) > -1)
			result.setText("创建成功");
		else
			result.setText("创建失败");
	}
}
